package com.ymatou.userregister.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.LoginAuthBean;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.userregister.parameter.YmatouUserRegBean;
import com.ymatou.userregister.service.YmatouUserRegCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.common.enums.UserTerminalSourceEnum;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 洋码头用户注册接口测试
 * @author chenshuangyan
 *
 */
public class Ts_YmatouUserReg {
	private static YmatouUserRegBean regBean;
	private static YmatouUserRegCall regCall;
	private static UserTokenWapper userTokenWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户注册 ");
	}

	@Before
	public void caseUp() throws Exception {
		regBean = new YmatouUserRegBean();
		regCall = new YmatouUserRegCall();
		userTokenWapper = new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(regCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_YmatouUserReg_001() {
		Logger.start(true,"注册用户成功");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("ok", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Map userToken = userTokenWapper.getUserTokenByUid(regCall.getUserId());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			
			Logger.comment("验证登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setClientId("123");
			lab.setClientIp("1.1.1.1");
			lab.setLoginId(loginId);
			lab.setPas("123456");
			lab.setSourceType("mainsite");
			lac.setData(false, lab);
			lac.callService();
			
			Logger.verifyEquals(regCall.getUserId(), lac.getUserData().getUserId(), "验证用户Id");
			Logger.verifyEquals(loginId.toLowerCase(), lac.getUserData().getLoginId().toLowerCase(), "验证登陆ID");
			Logger.verifyEquals(email, lac.getUserData().getLoginMail(), "验证email");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(regCall.getUserId());
			Logger.verifyIsNull(findUserByUid(regCall.getUserId()), "验证数据已删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_002() {
		Logger.start(false,"不输入loginId,注册失败");
		try {
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("登录ID不能为空", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(6d, regCall.getResult().get("VerifyCode"), "验证凭据(6表示登录ID不能为空)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_YmatouUserReg_003() {
		Logger.start(true,"不输入Mobile,注册成功");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("true", regCall.getString("Success"), "验证注册成功");
			Logger.verifyEquals("ok", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Thread.sleep(1000);
			Map userToken = userTokenWapper.getUserTokenByUid(regCall.getUserId());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			
			Logger.comment("验证登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setClientId("123");
			lab.setClientIp("1.1.1.1");
			lab.setLoginId(loginId);
			lab.setPas("123456");
			lab.setSourceType("mainsite");
			lac.setData(false, lab);
			lac.callService();
			
			Logger.verifyEquals(regCall.getUserId(), lac.getUserData().getUserId(), "验证用户Id");
			Logger.verifyEquals(loginId.toLowerCase(), lac.getUserData().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email, lac.getUserData().getLoginMail(), "验证email");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(regCall.getUserId());
			Logger.verifyIsNull(findUserByUid(regCall.getUserId()), "验证数据已删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_YmatouUserReg_004() {
		Logger.start(true,"不输入Email,注册成功");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("true", regCall.getString("Success"), "验证注册成功");
			Logger.verifyEquals("ok", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Thread.sleep(1000);
			Map userToken = userTokenWapper.getUserTokenByUid(regCall.getUserId());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			
			Logger.comment("验证登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setClientId("123");
			lab.setClientIp("1.1.1.1");
			lab.setLoginId(loginId);
			lab.setPas("123456");
			lab.setSourceType("mainsite");
			lac.setData(false, lab);
			lac.callService();
			
			Logger.verifyEquals(regCall.getUserId(), lac.getUserData().getUserId(), "验证用户Id");
			Logger.verifyEquals(loginId.toLowerCase(), lac.getUserData().getLoginId(), "验证登陆ID");
			Logger.verifyIsNull(lac.getUserData().getLoginMail(), "验证email");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(regCall.getUserId());
			Logger.verifyIsNull(findUserByUid(regCall.getUserId()), "验证数据已删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_005() {
		Logger.start(false,"既不输入Email也不输入mobile,注册失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("登录邮箱，手机都为空", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(19d, regCall.getResult().get("VerifyCode"), "验证凭据(19表示登录邮箱，手机都为空)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_006() {
		Logger.start(false,"输入空的密码,注册失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();		
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("登录密码不能为空", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(4d, regCall.getResult().get("VerifyCode"), "验证凭据(4表示登录密码不能为空)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_007() {
		Logger.start(false,"输入已使用过的loginId,注册失败");
		try {
			String loginId = findUserByUid(100).getlBills().getLoginId();
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("登录昵称已存在", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(11d, regCall.getResult().get("VerifyCode"), "验证凭据(11表示登录昵称已存在)");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_008() {
		Logger.start(false,"输入已使用过的Email,注册失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = findUserByUid(100).getlBills().getEmail();
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("邮箱地址已存在", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(1d, regCall.getResult().get("VerifyCode"), "验证凭据(1表示邮箱地址已存在)");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_009() {
		Logger.start(false,"输入已使用过的Mobile,注册失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			String mobile = findUserByUid(100).getlBills().getMobile();
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(mobile);
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("手机号码已存在", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(3d, regCall.getResult().get("VerifyCode"), "验证凭据(3表示手机号码已存在)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_YmatouUserReg_010() {
		Logger.start(false,"不同意注册协议,注册失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(false);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证注册失败");
			Logger.verifyEquals("请同意注册协议", regCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(16d, regCall.getResult().get("VerifyCode"), "验证凭据(16表示没有同意注册协议)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_YmatouUserReg_011() {
		Logger.start(true,"用户名小于4位失败");
		try {
			String loginId = TestDataManager.getRandomNum(2)+"a";
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("昵称长度4~20位", regCall.getString("LastErrorMessage"), "验证返回的LastErrorMessage信息");
			Logger.verifyEquals(ECode.ERROR400, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_YmatouUserReg_012() {
		Logger.start(true,"密码太过简单失败");
		try {
			String loginId = "RegTest_"+TestDataManager.getRandomNum(10);
			String email = TestDataManager.getRandomNum(10)+"@ymatou.com";
			
			regBean.setClientIp("1.1.1.1");
			regBean.setIsAgreeContract(true);
			regBean.setLoginId(loginId);
			regBean.setLoginMail(email);
			regBean.setLoginMobile(TestDataManager.getPhoneNum());
			regBean.setPas("123456");
			regBean.setRegisterType(0);
			regBean.setRegSource(1);
			regBean.setTokenTimeOut(2000L);
			regBean.setVisitLan(1);
			regBean.setTerminalSource(UserTerminalSourceEnum.Ios);
			regCall.setData(regBean);
			regCall.callService();			
			
			Logger.verifyEquals("ok", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Map userToken = userTokenWapper.getUserTokenByUid(regCall.getUserId());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			
			Logger.comment("验证登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setClientId("123");
			lab.setClientIp("1.1.1.1");
			lab.setLoginId(loginId);
			lab.setPas("123456");
			lab.setSourceType("mainsite");
			lac.setData(false, lab);
			lac.callService();
			
			Logger.verifyEquals(regCall.getUserId(), lac.getUserData().getUserId(), "验证用户Id");
			Logger.verifyEquals(loginId.toLowerCase(), lac.getUserData().getLoginId().toLowerCase(), "验证登陆ID");
			Logger.verifyEquals(email, lac.getUserData().getLoginMail(), "验证email");
			
			Logger.comment("恢复数据，删除注册账号");
			deleteUserByUserId(regCall.getUserId());
			Logger.verifyIsNull(findUserByUid(regCall.getUserId()), "验证数据已删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 根据UserID删除注册用户
	 * @param loginId
	 * @throws Exception 
	 */
	private void deleteUserByUserId(int uid) throws Exception{
		ULC201406Wapper wapper = new ULC201406Wapper();
		wapper.deleteData("{\"uId\":" + uid + "}");
	}
	
	/**
	 * 判断用户是否存在
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	private ULC201406Bean findUserByUid(int uid) throws Exception{
		ULC201406Wapper wapper = new ULC201406Wapper();
		ULC201406Bean bean = wapper.getULCInfoByUid(uid);
		return bean;
	}
}
